﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


//IMPORTAMOS 
using EventSys.BussinesEntities;
using EventSys.BussinesLogic;
using Microsoft.Practices.Unity;

namespace EventSys.MVC4Web.Controllers
{
    public class RolesController : Controller
    {
        //
        // GET: /Roles/
        [Dependency]
        public IRolesService rolesService { get; set; }

        public ActionResult Index()
        {
            var lista = rolesService.MostrarRoles();
            return View(lista);
        }

        public ActionResult NuevoRol()
        {            
            return View();
        }

        [HttpPost]
        public ActionResult NuevoRol(webpages_Roles roles)
        {
            try
            {
                // TODO: Add insert logic here
                rolesService.AddRoles(roles);

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ViewData["message"] = ex.Message;
                return View(roles);
            }
        }

        public ActionResult Create()
        {
            return PartialView("_Create");
        }
        [HttpPost]
        public ActionResult Create(webpages_Roles roles)
        {
            var msg = "ERROR";
            var rpt = new { ok = false, msg = "  " + msg + ",   El Nombre este Repetido" };
            try
            {
                if (validarRol(roles) != true)
                {
                    rolesService.AddRoles(roles);
                    rpt = new
                    {
                        ok = true,
                        msg = "Se registro Correctamente"
                    };
                }
                return Json(rpt, JsonRequestBehavior.AllowGet);//nos permite enviar el mensaje
            }
            catch (SystemException)
            {
                return Json(rpt, JsonRequestBehavior.AllowGet);
            }
            //rolesService.AddRoles(roles);
            //return RedirectToAction("Index", "Roles");
        }

        public ActionResult Edit(int id)
        {
            var rol = rolesService.GetRolesById(id);
            return View(rol);
        }

        [HttpPost]
        public ActionResult Edit(int id,webpages_Roles roles)
        {
            try
            {
                // TODO: Add update logic here
                rolesService.UpdateRoles(roles);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ViewData["message"] = ex.Message;
                return View(roles);
            }
        }
        public ActionResult Delete(int id)
        {
            rolesService.RemoveRoles(id);
            return RedirectToAction("Index");            
        }

        /// ///////para validar el rOL NO SE REPITA/////
        public bool validarRol(webpages_Roles roles)
        {
            var list = rolesService.MostrarRoles().ToList();
            try
            {
                var query = from c in list select c.RoleName;

                foreach (var item in query)
                {
                    if (item == roles.RoleName)
                    {
                        return true;
                    }
                }
            }
            catch (SystemException) { }
            return false;
        }


    }
}
